﻿
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Windows.Forms;
using DataSynchronizer;
using GenericEntities;
using GoogleAdapter;
using System.Collections;

namespace DiaryPlanner
{
    class CalendarConnection
    {
        private static CalendarConnection instance;
        SyncService service;
        public Boolean isLogin { get; set; }
        IList dellist = new List<GenericEvent>();

        public CalendarConnection()
        {

        }

        public Boolean login(string id, string pass)
        {
            service = new SyncService(id,pass);

            if (service.getLogin())
            {
                CalendarView.viewAll();

                return isLogin = true;
            }
            else
            {
                MessageBox.Show("아이디 또는 비밀번호가 잘못되었습니다.");
                return isLogin = false;
            }
        }

        public static CalendarConnection Instance()
        {
            if (instance == null) instance = new CalendarConnection();
            return instance;
        }
        
        public void syncCalandar()
        {
            if (!UserProfile.Instance().connection || !isLogin) return;
            bool isItem = false;

            IList google = service.SynchronizeEvents();
            IList db = CalendarDB.Instance().loadAll_db();

            
            for (int i = 0; i < google.Count; i++)
            {
                for (int j = 0; j < db.Count; j++)
                {
                    if( ((GenericEvent)google[i]).Title == ((GenericEvent)db[j]).Title &&
                        ((GenericEvent)google[i]).StartTime == ((GenericEvent)db[j]).StartTime &&
                        ((GenericEvent)google[i]).EndTime == ((GenericEvent)db[j]).EndTime &&
                        ((GenericEvent)google[i]).Contents == ((GenericEvent)db[j]).Contents)
                    {
                        isItem = true;
                        break;
                    }
                }
                if (!isItem) CalendarDB.Instance().save_db((GenericEvent)google[i]);
                else isItem = false;
            }

            for (int i = 0; i < db.Count; i++)
            {
                for (int j = 0; j < google.Count; j++)
                {
                    if (((GenericEvent)google[j]).Title == ((GenericEvent)db[i]).Title &&
                        ((GenericEvent)google[j]).StartTime == ((GenericEvent)db[i]).StartTime &&
                        ((GenericEvent)google[j]).EndTime == ((GenericEvent)db[i]).EndTime &&
                        ((GenericEvent)google[j]).Contents == ((GenericEvent)db[i]).Contents)
                    {
                        isItem = true;
                        break;
                    }
                }
                if (!isItem) addCalendar((GenericEvent)db[i]);
                else isItem = false;
            }
            
        }

        public void addCalendar(GenericEvent genericEvent)
        {
            if (!UserProfile.Instance().connection) return;
            service.addCalendar(genericEvent);
        }

        public void updateCalendar(GenericEvent oldGe, GenericEvent newGe)
        {
            if (!UserProfile.Instance().connection) return;
            service.updateCalendar(oldGe,newGe);
        }

        public void delCalendar(GenericEvent genericEvent)
        {
            if (!UserProfile.Instance().connection) return;
            service.delCalendar(genericEvent);
        }

        public void clearAllCalendar()
        {
            service.delAllCalendar();
        }
    }
}
